package gspushmq.v1.subscribe.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;

import gspushmq.model.ApiMessage;
import gspushmq.model.ApiMessageType;
import gspushmq.util.GGLogger;
import gspushmq.util.SynUtil;
import gspushmq.v1.subscribe.GGSubscribe;
import gspushmq.v1.subscribe.handler.core.GGApiHandler;

/**
 * accountId有关联的消息
 * @version 1.0
 * @since JDK1.7
 * @author yutao
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月19日上午11:11:19
 */
public class GGAccountIdsHandlerImpl extends GGApiHandler {

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void handler() {
		ApiMessageType apiType = getApiType();
		
		String mqSubType = apiType.getMq_sub_type();
		
		JSONArray array = getArray();
		if(array == null || array.isEmpty()) {
			return;
		}
		//已经在处理里 所以移除掉
		GGSubscribe.apiHandlerQueue.remove(apiType);
		
		Object obj = array.get(0);
		if(obj == null) {
			return;
		}
		
		if(!(obj instanceof Map)) {
			return;
		}
		
		Map jsonObj = (Map)obj;
		//一类消息  对应的  用户集合   
		Set<ApiMessage> set = GGSubscribe.ApiUrlMessage.get(apiType);
		if(set == null) {
			return;
		}
		
		for(ApiMessage apiMess : set) {
			
			String accountId = apiMess.getAccount_id();
			
			Object objList = jsonObj.get(accountId);
			if(objList == null) {
				continue;
			}
			
			List<Map<String, Object>> newsList = (List<Map<String, Object>>)objList;
			
			/*DataBase dataBase = GGDataStorage.ApiBaseMap.get(apiType);
			if(dataBase == null) {
				dataBase = new DataBase();
				GGDataStorage.ApiBaseMap.put(apiType, dataBase);
			}
			
			ConcurrentHashMap<String, Object> baseAidMap = dataBase.getAccountIdsMap();
			if(baseAidMap == null) {
				baseAidMap = new ConcurrentHashMap<>();
				dataBase.setAccountIdsMap(baseAidMap);
			}*/
			Map<String, Object> baseAidMap = SynUtil.synApiCommon(apiType);
			
			Object pushList = null;
			List<Map<String,Object>> subList = new ArrayList<>(newsList);
			
			String messageFlag = apiType.getMessageFlag();
			String baseKey = accountId + "|" + messageFlag;
			
			Object baseObj = baseAidMap.get(baseKey);
			
			if(baseObj != null) {
				if(baseObj instanceof List) {
					List<Map<String, Object>> baseList = (List<Map<String, Object>>)baseObj;
					GGLogger.warn(getClass(), "---api---handler--basedata--" + baseList + "--accountId--" + accountId);
					int size = baseList.size();
					int newSize = newsList.size();
					if(newSize > size) {
						int n =  newSize - size;
						subList = subList.subList(0, n);
					}else {
						//没有数据的变化就什么都不做
						objList = null;
						subList = null;
						continue;
					}
				}
			}
			
			pushList = subList;
			
			if(pushList == null) {
				objList = null;
				continue;
			}
			
			if("count".equals(mqSubType)) {
				pushList = ((List)newsList).size();
			}
			
			//更新比较数据
			baseAidMap.put(baseKey, newsList);
			
			//将处理好的数据记录到生产表
			handlerData(apiMess, pushList, "多账号推送列表");
			//标记给推送程序
			GGSubscribe.apiActiveQueue.add(apiType);
		}
	}
}
